草庐IT

C++ std::atomic 与 Boost atomic

全部标签

c++ - 编译时的 std::experimental::source_location

std::experimental::source_location可能会在某个时候添加到C++标准中。我想知道是否有可能将位置信息获取到编译时领域。本质上,我想要一个在从不同源位置调用时返回不同类型的函数。像这样的东西,虽然它没有编译因为location对象不是constexpr因为它是一个函数参数:#includeusingnamespacestd::experimental;constexprautoline(constsource_location&location=source_location::current()){returnstd::integral_constant

c++ - 无法手动调用 std::string 的析构函数

我正在试验union,并将这个示例设为A类,其中包含匿名union成员。由于union包含一个std::string和一个std::vector我需要为该类定义一个析构函数。但是,当我尝试手动调用~string()时,我得到了union.cpp:Indestructor'A::~A()':union.cpp:14:14:error:expectedclass-namebefore'('tokens_.~string();我不明白这个vector。如果我删除对s._~string();的调用,它可以正常编译。这是编译器错误吗?我正在使用MinGW64。#include#includecl

c++ - 比较 std::vector 在命名空间中使用自己的类不编译

以下代码无法编译,因为未找到比较运算符。#include#include#includenamespaceCool{structPerson{std::stringname;};}booloperator==(constCool::Person&p1,constCool::Person&p2){returnp1.name==p2.name;}intmain(int,char*[]){std::vectora{{"test"}};std::vectorb{{"test"}};boolok=a==b;std::cout经过一些实验,我发现以下代码可以完美编译:#include#includ

c++ - 为什么 volatile 不是 sig_atomic_t 的一部分

在我的平台(X86、Fedora、gcc9.1.1)上,sig_atomic_t类型定义为普通的int。在C++标准中,sig_atomic_t始终与volatile限定符一起使用。我明白为什么需要volatile,但为什么它不是类型的一部分呢?类似于:usingsig_atomic_t=volatileint; 最佳答案 这是从C继承的。C定义,同时允许sig_atomic_t要volatile合格,不需要它。我看过的标准文档(N1570)中使用的所有示例都以volatilesig_atomic_t的形式给出。.现在使用std:a

C++ 在 priority_queue 中使用 std::greater() 并排序

为什么这两种情况的文档说的是同一件事,但它们以相反的方式声明,一个使用greater而另一个使用greater().任何人都可以解释一下吗?文档priority_queuecpplibrary说那个compcanbeComparisonobjecttobeusedtoordertheheap.Thismaybeafunctionpointerorfunctionobjectpriority_queue,greater>minheap;//workspriority_queue,greater()>minheap;//whyfail?文档cpplibrarysort说的是同一件事,即co

c++ - 如何在 C++11 中返回包含自定义删除器的 std::unique_ptr?

我的应用程序编译器最多只能支持c++11。下面是我的项目和函数get_conn()的片段代码返回std::unique_ptr和自定义删除器(删除器需要两个参数)。我正在使用auto关键字作为返回类型,但它给出了一个错误,就像ifiscompiledwithc++11(compilesfinewithc++14)error:‘get_conn’functionuses‘auto’typespecifierwithouttrailingreturntype演示示例代码:#include#include#includeusingnamespacestd;//Dummydefinitiono

c++ - 如何将元素从 std::list 复制到结构数组?

我需要将std::list的内容复制到数组中,其中数组是数组的结构。下面是它的代码实现。#include#includeusingnamespacestd;typedefstruct{intheight;intwidth;intlength;}dimensions;GetDimensions(list,*int);//Functionthatcopiesthecontentoflisttoarraypassedassecondparameterintmain(){dimensionscuboid[10];intplane[10];listplaneList=GetList();//Fu

c++ - 如何调用对象的成员函数作为 std 算法的 unary_function?

我有一个看起来像这样的类。classA{public:voiddoSomething();}我有一组这些类。我想对数组中的每个项目调用doSomething()。使用算法header执行此操作的最简单方法是什么? 最佳答案 使用std::mem_fun_ref将成员函数包装为一元函数。#include#includestd::vectorthe_vector;...std::for_each(the_vector.begin(),the_vector.end(),std::mem_fun_ref(&A::doSomething));

C++ std::transform 副作用

我已经实现了这样的UnaryOperationstructConverter{Converter(std::size_tvalue):value_(value),i_(0){}std::stringoperator()(conststd::string&word){return(value_&(1我喜欢用它std::vectorv;//initializationofvstd::transform(v.begin(),v.end(),std::back_inserter(result),Converter(data));我的问题是我能否依赖我的假设,即算法将按照“Converter::

c++ - 使用构造函数向 std::set 插入一个元素

是否可以向std::set插入一个新元素,例如std::list的情况://插入一个名为“string”的元素到mylist的子列表std::list>mylist;mylist.push_back(std::list(1,"string"));现在,mylist在它的std::list类型的子列表中有一个std::string类型的元素。如果std::set是std::list的子集我的列表即std::list>mylist;如果你不能,那为什么不呢? 最佳答案 我认为这应该可以解决问题:intmain(){strings="te